require.config({
	 shim:{
	 	"cookie":["jquery"],
	 	"fly":["jquery"],
		"requeset":["jquery"]
	 },
	paths:{
		"jquery":"jquery-1.11.3",
		"iconfont":"../iconfont/iconfont",
		"cookie":"jquery.cookie",
		"fly":"jquery.fly",
		"requeset":"chang"
	}
})
require(["jquery","iconfont","cookie","fly","requeset"],function($,iconfont,cookie,fly,requeset){
	$(function(){
			$("#det_chatop").load("../html/Top&Bottmo.html .top",function(){
		
		})
		 	$("#det_chabom").load("../html/Top&Bottmo.html .bottom")
		 	$("#det_chari").load("../html/Top&Bottmo.html .amj_r",function(){
				var l = `<script src="../js/Top&Bottom.js"></script>`
			$("body").append(l)
		})
		 	
	new Fang()
	
		 	$(".det_xuan li").hover(function(){
		 		$(this).find(".det_xuanxia").animate({"opacity":1},500)
		 		$(this).find(".detxuanicon").animate({"left":10,"opacity":"0"})
		 		$(this).find(".detxuanshang").animate({"left":175,"opacity":"0"})
		 	},function(){
		 		$(this).find(".det_xuanxia").animate({"opacity":0},500)
		 		$(this).find(".detxuanicon").animate({"left":60,"opacity":"1"})
		 		$(this).find(".detxuanshang").animate({"left":125,"opacity":"1"})
		 	})
		 	
		 	var arr = [2,5,6,8,14]
		$.get("../js/detal.json",function(str){
			 $(".det_tuip").click(function(){
			 	$(".det_duipin").html(null)
			 	 arr = []
			 	 for(var i =0;i<5;i++){
			 	 	arr.push(Math.round(Math.random()*20)+1)
			 	 }
			 	 fnppin()
			 })
			 fnppin()
			 function fnppin(){
			 	for(var i =0;i<arr.length;i++){
					$(".det_duipin").append(`<li>
							<img src="${str.huanpi[arr[i]].img}" />
							<p>${str.huanpi[arr[i]].p}</p>
							<h3>${str.huanpi[arr[i]].pic}</h3>
						</li>`) 	
			 }
			 }
		})
		 //滚动条改变样式	
		$(window).scroll(function(){
		   
			if($(document).scrollTop()> 1400){
				 $(".det_fudong").addClass("det_fudong2")
				 console.log(1)
			}else{
				$(".det_fudong").removeClass("det_fudong2")
			}
			
		 })	
		 	
		 	
		 //购物车功能
		 //增加
		 $("#amj_r_g span").html(loadCart());
		 $(".det_xia").click(function(){
		 	var picnum =  parseInt($(this).parent().find(".det_zhong").val())
		 	$(this).parent().find(".det_zhong").val(picnum+=1)
		 })
		 //减少
		 $(".det_shang").click(function(){
		 	var picnum =  parseInt($(this).parent().find(".det_zhong").val())
		 	if(picnum>1){
		 		$(this).parent().find(".det_zhong").val(picnum-=1)
		 	}
		 })
		 //修改
		 $(".det_zhong").blur(function(){
		 	 var picnum =  parseInt($(this).parent().find(".det_zhong").val())
		 	  var pattern = /^\^0[0-9]+$/;
			   if(!pattern.test(picnum)){
					 picnum = 1
					 $(this).parent().find(".det_zhong").val(picnum)
				}
		 })
		 //创建飞入
		 $('.det_maimai2').click(function(e){
		 	 var goodId = 100
		 	 var goodSrc = "../img/det4.jpeg"
		 	 var goodNei = "MeadJohnson美赞臣 幼儿助长奶粉 安儿宝Enfagrow 港版3段(1-3岁) 900g/罐*2"
		 	 var goodYuan = "580.00"
		 	 var goodJiang = "425.00"
		 	 var goodnum = $(".det_zhong").val()
		 	 var cartStr = $.cookie("good") ? $.cookie("good") : "";
		 	 var cartobj = strtoobj(cartStr)
		 	 cartobj[goodId] = {
		 			nei :goodNei,
		 			src :goodSrc,
		 			yuan:goodYuan,
		 			jiang:goodJiang,
		 			num :goodnum
		 		}
		 	 cartStr = objtostr(cartobj)
		 	 //console.log(cartStr)
		 	 $.cookie("good",cartStr,{expirex:7,path:"/"})
		 	 loadCart()
		  //飞入
		 	var cloneImg = $(".feifei").clone().css({"width":50,"height":50,"zIndex":"1000"});
					cloneImg.fly({
						start : {
							top : e.clientY,
							left : e.clientX
						},
						end :{
							top : 350,
							left : 1240,
							width:0,
							height:0
						},
						autoPlay : true,
						onEnd : function(){
							$("#amj_r_g span").html(loadCart());
							$("#amj_r_rbnum").find("span").html(loadCart())
	 		 	 			$("#amj_r_rbpic").find("span").html(manei())
							cloneImg.remove();
							var str = $.cookie("good")
							var strobj = strtoobj(str)
							if(str){
								$(".amj_r_rp").css({"display":"none"})
								var pan = false
			 				 	 $(".gouwuactive img").each(function(){
			 				 			if($(this).attr("pid") == goodId){
			 				 			$(this).parent().find("div section b").html(loadCart())
			 				 			pan = true
			 				 			}
			 				 	})
			 				 	 if(pan == false){
				 				 	 var ls = `<div class="gouwuactive">
										<img src="${goodSrc}" pid ="${goodId}"/>
										<div>
											<p>${goodNei}</p>
											<section>
												<span>￥${goodJiang}</span>
												<b>${goodnum}</b>
												<em>X</em>
											</section>
										</div>
									</div>`
						 		 	 $("#amj_r_r").append(ls)
			 				 	 }
							}
						}
					})
		 	
		 })

		 function strtoobj(str){
		 		if(!str){
		 			return {}
		 		}
		 		var goods = str.split("!")
		 		var obj ={}
		 		for(var i=0;i<goods.length;i++){
		 			var data = goods[i].split(",")
		 			obj[data[0]]={
		 				nei :data[1],
		 				src : data[2],
		 				yuan : data[3],
		 				jiang : data[4],
		 				num:parseInt(data[5])
		 			}
		 		}
		 		return obj
		 	}
		 	//-------将对象转化为字符串
		 	function objtostr(obj){
		 		 var strr =""
		 		 for(var id in obj){
		 		 	if(strr){
		 		 		strr += "!"
		 		 	}
		 		 	strr += id +","+obj[id].nei+","+obj[id].src+","+obj[id].yuan+","+obj[id].jiang+","+obj[id].num
		 		 }
		 		 return strr
		 	}
		 	//-----------------一共多少件
		 	function loadCart(){
				var cartStr = $.cookie("good") ? $.cookie("good") : "";
					var cartObj = strtoobj(cartStr);
					//获取到购物车中所有商品的数量
					var total = 0;
					for(var id in cartObj){
						total += cartObj[id].num;
						 
					}
					return total
			}
		 //------------------------一共多少钱
		  function manei(){
		 	var cartStr = $.cookie("good") ? $.cookie("good") : "";
					var cartObj = strtoobj(cartStr);
					//获取到购物车中所有商品的数量
					var total = 0;
					for(var id in cartObj){
						total += cartObj[id].num * cartObj[id].jiang;
						 
					}
					return total
		 }
		 	
		 	
		 	
		 	
		 	
		 	
		 	
		 	
		 	
		 	
		 	
		 	
	})
	
	
	
	
	
	
	
	
	
	
})
function Fang(){
		this.sli = $(".det_fangs li") //小图
		this.bli = $(".det_xianb img" )//中图
		this.fli = $(".det_fangb img")//大图
		this.zhe = $(".zhe")//遮盖
		this.dong =$(".dong")//移动
		this.init()
	}
	Fang.prototype.init=function(){
		 //划过变化图片
		 var that = this
		 //当前的默认第一个
		 this.index = 0
		 this.huali()
		 //点击效果
		 this.dianli()
		 //划入遮罩层
		 this.ruzhe()
		 //滑动放大
		 this.huada()
	}
	Fang.prototype.huali = function(){
		var that = this
		//划过都加class名
		this.sli.hover(function(){
			if($(this).index() == that.index){
				 $(this).addClass("detli")
				 $(this).find("span").addClass("detspan")
			}else{
				$(this).addClass("detli")
				 $(this).find("span").addClass("detspan")
				 $(this).find("img").addClass("detimg")
			}
			 
			 
		},function(){
			//划出不是当前下标的删除class名
			if($(this).index() == that.index){
				
			}else{
				$(this).removeClass("detli")
				$(this).find("span").removeClass("detspan")
				$(this).find("img").removeClass("detimg")
			}
		})
	}
	Fang.prototype.dianli= function(){
		var that = this
		this.sli.click(function(){
			//点击也会划过，所以直接删除不是当前下标的class名
			that.index = $(this).index()
			$(this).siblings().removeClass("detli")
			$(this).siblings().find("span").removeClass("detspan")
			$(this).siblings().find("img").removeClass("detimg")
			//中图显示,其他消失，但是不能让遮罩层消失
			that.bli.eq(that.index).parent().find("img").css({'display':"none"})
			that.bli.eq(that.index).css({'display':"block","zIndex":"5"})
			
			//大图显示
			that.fli.eq(that.index).css({'display':"block"})
			that.fli.eq(that.index).siblings().css({'display':"none"})
		})
	}
	
	Fang.prototype.ruzhe = function(){
		 var that =this
		 this.zhe.hover(function(){
		 	that.dong.css({"display":"block"})
		 	//大图出现
		 	$(".det_fangb").css({"display":"block"})
		 },function(){
		 	that.dong.css({"display":"none"})
		 	//大图出现
		 	$(".det_fangb").css({"display":"none"})
		 })
	}
	
	Fang.prototype.huada = function(){
		var that = this	 
		that.zhe.mousemove(function(event){
			var evt = event || window.event
			var $x = evt.offsetX - that.dong.width()/2
		 	var $y = evt.offsetY - that.dong.height()/2
		 	 if($x<0){
		 	 	$x =0
		 	 }
		 	 if($y<0){
		 	 	$y=0
		 	 }
		 	 if($x+that.dong.width() > that.zhe.width()){
		 	 	$x = that.zhe.width() - that.dong.width()
		 	 }
		 	 if($y + that.dong.height() > that.zhe.height()){
		 	 	$y = that.zhe.height() -that.dong.height()
		 	 }
		 	 //console.log(that.zhe.width())
		 	 //动图位置
		 	 that.dong.css({"top":$y,"left":$x})
		 	 //大图跟着变化
		 	 //console.log(that.dong.position().left)
		 	 that.fli.eq(that.index).css({
		 	 	"left":-that.dong.position().left / that.zhe.width()*780,
		 	 	"top":-that.dong.position().top / that.zhe.height()*780
		 	 })
		})
		
	}